package com.meringer.beans;

import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

import com.meringer.beans.ejb.RegisterEJB;

@RequestScoped
@ManagedBean(name = "registerBean")
public class RegisterBean {
	
	@EJB
	RegisterEJB userUtil;
	private String username;
	private String password;
	private Integer age;
	private String realname;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}
	
	public String savePerson() {
		System.out.println("wewfwefwwef");
//		System.out.println(FacesContext.getCurrentInstance().getMessages());
//		FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Test error", null));
		boolean userExists = userUtil.addUser(username, realname, password, age);
		System.out.println("ageee " + age);
		if(userExists){
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Username is busy", null));
			return null;
		}
		return "toHome";
	}
	
}
